package Aptech.DAL;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Aptech.DALInterface.RoomInterface;
import HibernateMappingPoJo.Rooms;
import HibernateMappingPoJo.Users;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;


import org.hibernate.criterion.Restrictions;

public class MgRoom implements RoomInterface {
	private SessionFactory sessFact;
    private Session sess;
    private Transaction tr;
	@Override
	public List<Rooms> getAllRoom() {
        List<Rooms> list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(Rooms.class).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }

        return list;
		
	}

	@Override
	public void addRoom(Rooms room) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();
            sess.save(room);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {

            System.out.println(he.getMessage());

        } finally {
            sess.close();

        }
		
	}

	@Override
	public void editRoom(Rooms room) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();

            sess.update(room);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
		
	}

	@Override
	public void deleteRoom(String id) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            String sql = "delete Rooms where ID = '" + id + "'";
            sess.createSQLQuery(sql).executeUpdate();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
		
	}

	@Override
	public Rooms getRoomByID(String id) {
		Rooms room = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            Criteria criteria = sess.createCriteria(Rooms.class).add(Restrictions.eq("id", id));
            room = (Rooms) criteria.uniqueResult();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return room;
    }

	@Override
	public List getRoomByBuilt(String idBuilt) {
		 
         List list = null;
     try {
         sessFact = Hibernate.HibernateUtil.getSessionFactory();
         sess = sessFact.openSession();
       

         Criteria cr = sess.createCriteria(Rooms.class);
         cr.add(Restrictions.eq("idBuilt", idBuilt));
         list = cr.list();
         if(list.isEmpty()) return null;
      
         sess.flush();
     } catch (HibernateException he) {
         System.out.println(he.getMessage());
     } finally {
         sess.close();        
     }
     return list;
	}
	}


